#include "ComponentShapeshifter.h"
#include "Components/ComponentCreature.h"
#include "Components/ComponentBody.h"
#include "Components/ComponentHealth.h"
#include "Components/ComponentSpawn.h"

#include "Components/ComponentCreatureSounds.h"
#include "ComponentPathfinding.h"
#include "Subsystems/SubsystemTerrain.h"
#include "Subsystems/SubsystemAnimalEntities.h"
#include "Subsystems/SubsystemGameInfo.h"
#include "Subsystems/SubsystemWeather.h"

#include "GameRandom.hpp"
#include "Engine/MathUtils.hpp"
#include "Log.h"

#include "GameSingleton.h"
#include "GameRegistry.hpp"
#include "GameThreadPool.hpp"

#include <iostream>

namespace PocketSurvival
{
    void ComponentShapeshifter::shapeshiftTo(const std::string &entityTemplateName)
    {
        if (entityTemplateName != "")
        {
            Vector3 position;
            componentBody->getPosition(position);

            GameSingleton::singletonPtr->getSingleton<SubsystemAnimalEntities>()->addAnimalEntity(entityTemplateName, position);
            componentSpawn->despawn();

            // 播放声音
        }
    }

    void ComponentShapeshifter::update(float dt)
    {
        bool areSupernaturalCreaturesEnabled = gameInfo->areSupernaturalCreaturesEnabled;
        if (isEnabled && isShapeshiftTo == false && componentHealth->health > 0.0f)
        {
            if (areSupernaturalCreaturesEnabled == false && m_dayEntityTemplateName != "")
            {
                shapeshiftTo(m_dayEntityTemplateName);
            }
            else if (subsystemWeather->skylightIntensity > 0.25f && m_dayEntityTemplateName != "")
            {
                m_timeToSwitch -= 2.0f * dt;
                if (m_timeToSwitch <= 0.0f)
                {
                    shapeshiftTo(m_dayEntityTemplateName);
                }
            }
            else if (areSupernaturalCreaturesEnabled && 
                subsystemWeather->skylightIntensity < 0.1f &&
                (subsystemWeather->moonPhase == 0 || subsystemWeather->moonPhase == 4) &&
                m_nightEntityTemplateName != "")
            {
                m_timeToSwitch -= dt;
                if (m_timeToSwitch <= 0.0f)
                {
                    shapeshiftTo(m_nightEntityTemplateName);
                }
            }
        }
    }

    static const std::string componentName = "Shapeshifter";
    const std::string &ComponentShapeshifter::getName() const
    {
        return componentName;
    }
    const std::string &ComponentShapeshifter::GetName()
    {
        return componentName;
    }
    void ComponentShapeshifter::load(const nlohmann::json &jsonData)
    {
        componentBody   = GameSingleton::gameRegistry->get<ComponentBody *>(entity);
        componentSpawn  = GameSingleton::gameRegistry->get<ComponentSpawn *>(entity);
        componentHealth = GameSingleton::gameRegistry->get<ComponentHealth *>(entity);

        gameInfo = GameSingleton::singletonPtr->getSingleton<SubsystemGameInfo>();
        subsystemWeather = GameSingleton::singletonPtr->getSingleton<SubsystemWeather>();

        m_dayEntityTemplateName = jsonData["DayEntityTemplateName"].get<std::string>();
        m_nightEntityTemplateName = jsonData["NightEntityTemplateName"].get<std::string>();
        float value = jsonData["Probability"].get<float>();
        m_timeToSwitch = GameSingleton::gameRandom->getFloat(3.0f, 15.0f);
        isEnabled = GameSingleton::gameRandom->getFloat(0.0f, 1.0f) < value;
    }
}
